package singlecase;

import java.util.HashMap;

/**
 * @Description:
 * @Author: csh
 * @CreateDate: 2018/8/20$ 10:58$
 * @Version: 1.0
 */
public class HashMapSingleton {
    /**用来存放对应的关系*/
    private static HashMap sinRegistry = new HashMap();
    /**实例化对象*/
    static private HashMapSingleton singleton = new HashMapSingleton();
    /**受保护的构造函数*/
    protected HashMapSingleton(){

    }

    public static HashMapSingleton getSingleton(String name) {
        if(name==null){
            name ="singleton";
        }
        if(sinRegistry.get(name)==null){
            try {
                sinRegistry.put(name, Class.forName(name).newInstance());
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return (HashMapSingleton)(sinRegistry.get(name));
    }

    public void test(){
        System.out.println("getclasssucess!");
    }
}

class SingletonChild1 extends HashMapSingleton{
    public SingletonChild1(){}

    static public SingletonChild1 getInstance(){
        return (SingletonChild1)HashMapSingleton.getSingleton("SingletonChild1");
    }

    @Override
    public void test(){
        System.out.println("getclasssucess!1111");
    }
}
